package menu

import (
	"github.com/gin-gonic/gin"
	"github.com/xinglou123/pkg/os/conv"
	"github.com/xinglou123/pkg/utils"
	"net/http"
)

type ApiMenu struct {
}

func (au ApiMenu) Router(router *gin.Engine) {
	r := router.Group("menu")
	r.GET("list", au.menuList)
	r.GET("tree", au.menuTree)
	r.POST("edit", au.menuEdit)
	r.POST("delete/:id", au.menuDelete)
	r.POST("create", au.menuAdd)

}

// @Title /menu/list
// @API_GROUP user
// @Description 查询列表接口
// @Param   title     query   int true       "title"
// @Param   page     query   int true       "page"
// @Param   pageSize     query   int true       "pageSize"
// @Success 200 {object}
// @router /menu/list [get]
func (au ApiMenu) menuList(ctx *gin.Context) {
	result := utils.NewResponse()
	defer ctx.JSON(http.StatusOK, result)

	params := map[string]interface{}{}
	params["limit"] = ctx.DefaultQuery("limit", "10")
	params["page"] = ctx.DefaultQuery("page", "1")

	list, page, err := MenuService.Query(params)
	if err != nil {
		result.Code = 100
		result.Msg = err.Error()
		return
	}
	data := map[string]interface{}{}
	data["lists"] = list
	data["page"] = page
	result.Data = data
	result.Code = 200
	result.Msg = "ok"

	return

}

// @Title /menu/tree
// @API_GROUP menu
// @Description 查询列表接口
// @Success 200 {object}
// @router /menu/tree [get]
func (au ApiMenu) menuTree(ctx *gin.Context) {
	result := utils.NewResponse()
	defer ctx.JSON(http.StatusOK, result)

	list := MenuService.MenuTree()

	result.Data = list
	result.Code = 200
	result.Msg = "ok"

	return

}

// @Title menu add
// @API_GROUP menu
// @Description 添加接口
// @Param   SignupdateForm
// @Success 200 {object}
// @router /menu/add [post]
func (au ApiMenu) menuAdd(ctx *gin.Context) {
	result := utils.NewResponse()
	defer ctx.JSON(http.StatusOK, result)

	var user Menu
	if error := ctx.Bind(&user); error != nil {
		result.Code = 100
		result.Msg = "参数异常"
		return
	}
	uuser, err := MenuService.Add(user)
	if err != nil {
		result.Code = 100
		result.Msg = err.Error()
		return
	}
	result.Msg = "添加成功"
	result.Data = uuser
	return

}

// @Title menu Delete
// @API_GROUP menu
// @Description 删除接口
// @Param   id     query   int true       "id"
// @Success 200 {object}
// @router /menu/delete [post]
func (au ApiMenu) menuDelete(ctx *gin.Context) {
	result := utils.NewResponse()
	defer ctx.JSON(http.StatusOK, result)

	uid := ctx.Param("id")
	if uid == "" {
		result.Code = 100
		result.Msg = "参数异常"
		return
	}
	_, err := MenuService.Delete(conv.Int64(uid))
	if err != nil {
		result.Code = 100
		result.Msg = "删除失败"
		return
	}
	result.Msg = "删除成功"
	return
}

// @Title menu edit
// @API_GROUP menu
// @Description 编辑接口
// @Param   SignupdateForm
// @Success 200 {object}
// @router /menu/edit [post]
func (au ApiMenu) menuEdit(ctx *gin.Context) {
	result := utils.NewResponse()
	defer ctx.JSON(http.StatusOK, result)

	var menu Menu
	if err := ctx.Bind(&menu); err != nil {
		result.Code = 100
		result.Msg = err.Error()
		return
	}
	if menu.Id == 0 {
		result.Code = 100
		result.Msg = "请选择要修改的记录"
		return
	}
	_, err := MenuService.Update(menu)
	if err != nil {
		result.Code = 100
		result.Msg = "修改失败"
		return
	}
	result.Msg = "修改成功"
	return
}
